package com.hzhc.service.impl;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.SmsLog;
import com.hzhc.entity.SmsTemplate;
import com.hzhc.mapper.SmsLogMapper;
import com.hzhc.mapper.SmsTemplateMapper;
import com.hzhc.service.SmsTemplateService;
import com.hzhc.utils.AliyunSendSmsUtil;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TxSendSmsUtil;
import com.hzhc.vo.SmsTemplateVo;
import com.tencentcloudapi.sms.v20210111.models.SendStatus;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author dongcy
 * @date 2024/6/17 10:33
 * @description SmsTemplateServiceImpl
 */
@Service
@RequiredArgsConstructor
public class SmsTemplateServiceImpl extends ServiceImpl<SmsTemplateMapper, SmsTemplate> implements SmsTemplateService {

    private final SmsLogMapper smsLogMapper;

    @Override
    public List<SmsTemplateVo> getMessageTemplateList(Page<SmsTemplateVo> page, SmsTemplateVo vo) {
        IPage<SmsTemplateVo> iPage = baseMapper.getMessageTemplateList(page,vo);
        return iPage.getRecords();
    }

    @Override
    public ResultUtil<T> addMessageTemplate(SmsTemplateVo vo) {
        SmsTemplate template = new SmsTemplate();
        BeanUtils.copyProperties(vo,template);
        template.setCreateTime(LocalDateTime.now());
        boolean b = save(template);
        if (b){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> deleteMessageTemplate(Long id) {
        SmsTemplate template = getById(id);
        template.setDisable(1);
        boolean b = updateById(template);
        if (b){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> updateMessageTemplate(SmsTemplateVo vo) {
        SmsTemplate template = getById(vo.getId());
        template.setApiTemplateCode(vo.getApiTemplateCode());
        template.setOpenStatus(vo.getOpenStatus());
        template.setRemark(vo.getRemark());
        template.setSmsChannelId(vo.getSmsChannelId());
        template.setSysDictId(vo.getSysDictId());
        template.setTemplateContent(vo.getTemplateContent());
        template.setTemplateName(vo.getTemplateName());
        template.setUpdateUserId(vo.getUpdateUserId());
        template.setUpdateTime(LocalDateTime.now());
        boolean b = updateById(template);
        if (b){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    @Override
    public List<SmsTemplateVo> getMsgTemplateList(SmsTemplateVo vo) {
        return baseMapper.getMsgTemplateList(vo);
    }

    @Override
    public boolean sendSms(String phone, String param, String[] params, String name) {

        SmsTemplateVo vo = baseMapper.getMsgTemplateListByTempName(name);
        if (vo == null) {
            return false;
        }
        if ("阿里云".equals(vo.getSmsChannelName())) {
            SendSmsResponse sendSmsResponse = AliyunSendSmsUtil.updateSendSms(vo, phone, param);
            SmsLog smsLog = new SmsLog();
            BeanUtils.copyProperties(vo, smsLog);
            smsLog.setCreateTime(LocalDateTime.now());
            smsLog.setSmsSendMobile(phone);
            if(sendSmsResponse != null){
                smsLog.setSmsResponseStatus(sendSmsResponse.getCode());
                smsLog.setSmsResponseMsg(sendSmsResponse.getMessage());
            }else{
                smsLog.setSmsResponseStatus("error");
                smsLog.setSmsResponseMsg("发送失败");
            }
            smsLogMapper.insert(smsLog);
            return true;
        }else {
            String[] phoneNumberSet = {"+86" + phone};
            com.tencentcloudapi.sms.v20210111.models.SendSmsResponse sendSmsResponse = TxSendSmsUtil.sendSms(vo, phoneNumberSet, params);
            SmsLog smsLog = new SmsLog();
            BeanUtils.copyProperties(vo, smsLog);
            smsLog.setCreateTime(LocalDateTime.now());
            smsLog.setSmsSendMobile(phone);
            if (sendSmsResponse != null) {
                SendStatus[] sendStatusSet = sendSmsResponse.getSendStatusSet();
                smsLog.setSmsResponseStatus(sendStatusSet[0].getCode());
                smsLog.setSmsResponseStatus(sendStatusSet[0].getMessage());
            }else{
                smsLog.setSmsResponseStatus("error");
                smsLog.setSmsResponseMsg("发送失败");
            }
            smsLogMapper.insert(smsLog);
            return true;
        }
    }
}
